package org.bbelovic.devel.exercises.collatz;

import java.util.ArrayList;
import java.util.List;

/**
 * Collatz problem and various exercises related to it.
 */
public class CollatzProblem {

    public List<Integer> computeCollatzNumbers(int n) {
        final List<Integer> result = new ArrayList<Integer>();
        result.add(n);
        while (n != 1) {
            n = collatzInternal(n);
            result.add(n);
        }
        return result;
    }
    
    public int getMaxValueFromCollatzNumbers(int n) {
        int max = 0;
        while (n != 1) {
            n = collatzInternal(n);
            if (max < n) max = n;
        }
        return max;
    }
    
    public int getNumberOfStepsForCollatzNumbers(int n) {
        int stepsNo = 0;
        while (n != 1) {
            n = collatzInternal(n);
            stepsNo++;
        }
        return stepsNo;
    }

    private int collatzInternal(int n) {
        if (n % 2 == 0) {
            n /= 2;
        } else {
            n = 3*n + 1;
        }
        return n;
    }
}
